Page({
    data: {
        phone: '', //供应商电话
        code: '', //验证码
        codeErr: false,
        password: '', //新密码
        passwordErr: false,
        password_confirm: '', //确认新密码
        password_confirmErr: false,
        buttonLoading: false,
        enableSendCode: true, //允许发送验证码
        text: '发送',
    },
    onLoad: function(options) {
        this.setData({ phone: options.phone })
    },
    /**获取验证码 */
    getCode() {
        if (!this.data.enableSendCode) return
        const { phone } = this.data
        if (phone === '') return wx.showToast({
            title: '手机号不存在',
            icon: 'none'
        })
        this.data.enableSendCode = false
        this.request({
            url: '/supplier/sms',
            data: { mobile: phone }
        }, () => {
            wx.showToast({
                title: '短信验证码已成功发送至您的手机，请注意查看',
                icon: 'none',
                duration: 3000
            })
            this.setContDown()
        }, () => {
            this.data.enableSendCode = true
        })
    },
    //设置倒计时
    setContDown() {
        this.setData({ text: '60s' })
        let time = 60
        const timeId = setInterval(() => {
            time--
            if (time <= 0) {
                this.data.enableSendCode = true
                this.setData({ text: '发送' })
                return clearInterval(timeId)
            }
            this.setData({ text: time + 's' })
        }, 1000)
    },
    confirm() {
        const { password, password_confirm, code } = this.data
        if (code === '') return this.setData({ codeErr: true })
        if (password === '') return this.setData({ passwordErr: true })
        if (password_confirm === '') return this.setData({ password_confirmErr: true })
        if (password !== password_confirm) return wx.showToast({
            title: '两次密码输入不一致',
            icon: 'none'
        })
        this.setData({ buttonLoading: true })
        this.request({
            url: '/supplier/supplier/editPasswd',
            method: 'POST',
            data: { sms_code: code, password }
        }, () => {
            wx.showToast({
                title: '修改成功',
                icon: 'success',
                mask: true
            })
            this.delay(() => {
                wx.navigateBack()
            })
        }, () => {
            this.setData({ buttonLoading: false })
        })
    }
})